/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frontera;

import entidad.Producto;
import entidad.Usuario;
import java.util.ArrayList;
import javax.swing.AbstractListModel;

/**
 *
 * @author Andrés
 */
public class AdministarProductos extends javax.swing.JPanel {

    /**
     * Creates new form AdministarProductos
     */
    private VentanaPrincipal ventanaP;
    private Producto seleccionado;
    
    public AdministarProductos(VentanaPrincipal v) {
        initComponents();
        
        this.ventanaP=v;
        contenido.remove(panelDerecho);
        panelDerecho = new VisualizarProductos(this);
        contenido.add(panelDerecho);
        
        Usuario usuarioActual = VentanaPrincipal.controlUsuario.getUsuarioActual();
        if(!usuarioActual.getPermisos().puedeAgregarProductos()) {
            agregarB.setVisible(false);
        }
        if(!usuarioActual.getPermisos().puedeEditarProductos()) {
            editarB.setVisible(false);
        }
        if(!usuarioActual.getPermisos().puedeEliminarProductos()) {
            eliminarB.setVisible(false);
        }
        actualizarResultados();
    }

    public Producto getSeleccionado() {
        return seleccionado;
    }

    public VentanaPrincipal getMainFrame() {
        return ventanaP;
    }
    
    public void actualizarResultados(){
        editarB.setSelected(false);
        resultados.setModel(new AbstractListModel() {
            ArrayList<Producto> listaR = VentanaPrincipal.controlProducto.buscar(buscarTF.getText());
            
            @Override
            public int getSize() {
                return listaR.size();
            }

            @Override
            public Object getElementAt(int index) {
                return listaR.get(index);
            }
        });
        
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titulo = new javax.swing.JLabel();
        imagen = new javax.swing.JLabel();
        contenido = new javax.swing.JPanel();
        leftPanel = new javax.swing.JPanel();
        sp = new javax.swing.JScrollPane();
        resultados = new javax.swing.JList();
        agregarB = new javax.swing.JButton();
        editarB = new javax.swing.JToggleButton();
        eliminarB = new javax.swing.JButton();
        buscarTF = new javax.swing.JTextField();
        panelDerecho = new javax.swing.JPanel();

        titulo.setFont(new java.awt.Font("Segoe UI", 1, 24)); // NOI18N
        titulo.setForeground(new java.awt.Color(0, 204, 0));
        titulo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        titulo.setText("Productos");

        imagen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/icono2.png"))); // NOI18N

        contenido.setLayout(new java.awt.GridLayout(1, 0));

        leftPanel.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(51, 204, 0)));

        resultados.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        resultados.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "-" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        resultados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                resultadosMouseClicked(evt);
            }
        });
        sp.setViewportView(resultados);

        agregarB.setBackground(new java.awt.Color(0, 204, 0));
        agregarB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/plusprod.png"))); // NOI18N
        agregarB.setPreferredSize(new java.awt.Dimension(30, 30));
        agregarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarBActionPerformed(evt);
            }
        });

        editarB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/EP.png"))); // NOI18N
        editarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editarBActionPerformed(evt);
            }
        });

        eliminarB.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/BP.png"))); // NOI18N
        eliminarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarBActionPerformed(evt);
            }
        });

        buscarTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        buscarTF.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                buscarTFKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout leftPanelLayout = new javax.swing.GroupLayout(leftPanel);
        leftPanel.setLayout(leftPanelLayout);
        leftPanelLayout.setHorizontalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(leftPanelLayout.createSequentialGroup()
                        .addComponent(agregarB, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buscarTF, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editarB, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eliminarB, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(sp))
                .addContainerGap())
        );
        leftPanelLayout.setVerticalGroup(
            leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(leftPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(agregarB, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editarB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(eliminarB, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buscarTF))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sp, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                .addContainerGap())
        );

        contenido.add(leftPanel);

        panelDerecho.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(51, 204, 0)));

        javax.swing.GroupLayout panelDerechoLayout = new javax.swing.GroupLayout(panelDerecho);
        panelDerecho.setLayout(panelDerechoLayout);
        panelDerechoLayout.setHorizontalGroup(
            panelDerechoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 288, Short.MAX_VALUE)
        );
        panelDerechoLayout.setVerticalGroup(
            panelDerechoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 245, Short.MAX_VALUE)
        );

        contenido.add(panelDerecho);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(titulo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(imagen))
                    .addComponent(contenido, javax.swing.GroupLayout.DEFAULT_SIZE, 581, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(titulo)
                    .addComponent(imagen))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(contenido, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void agregarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarBActionPerformed
        seleccionado = null;
        VisualizarProductos aux = (VisualizarProductos) panelDerecho;
        aux.limpiar();
        aux.habilitarTodo();
    }//GEN-LAST:event_agregarBActionPerformed

    private void editarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editarBActionPerformed
        if(seleccionado!=null){
            VisualizarProductos aux = (VisualizarProductos) panelDerecho;
            if(editarB.isSelected()) aux.habilitarTodo();
            else aux.deshabilitarTodo();
        } else editarB.setSelected(false);
    }//GEN-LAST:event_editarBActionPerformed

    private void buscarTFKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_buscarTFKeyReleased
        actualizarResultados();
    }//GEN-LAST:event_buscarTFKeyReleased

    private void resultadosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resultadosMouseClicked
        seleccionado = (Producto) resultados.getSelectedValue();
        if(seleccionado!=null){
            VisualizarProductos aux = (VisualizarProductos) panelDerecho;
            aux.visualizar(seleccionado);
            aux.deshabilitarTodo();
        }
    }//GEN-LAST:event_resultadosMouseClicked

    private void eliminarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarBActionPerformed
        if(seleccionado != null){
            VentanaPrincipal.controlProducto.eliminar(seleccionado);
            actualizarResultados();
            ((VisualizarProductos) panelDerecho).limpiar();
        }
    }//GEN-LAST:event_eliminarBActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton agregarB;
    private javax.swing.JTextField buscarTF;
    private javax.swing.JPanel contenido;
    private javax.swing.JToggleButton editarB;
    private javax.swing.JButton eliminarB;
    private javax.swing.JLabel imagen;
    private javax.swing.JPanel leftPanel;
    private javax.swing.JPanel panelDerecho;
    private javax.swing.JList resultados;
    private javax.swing.JScrollPane sp;
    private javax.swing.JLabel titulo;
    // End of variables declaration//GEN-END:variables
}
